/**
 * 文件概述
 * 详细介绍
 * @public
 * @author 杨兴潮
 * @version  v5.0
 * @copyright  南京海泰医疗信息系统有限公司
 * @team  智慧物联产品部
 * @date 2024/1/10 13:32:12
 */

export default {
  title:"js调用摄像头案例",
  name: "openCamera",
  components: {
  },
  data (){
    return {
      stream:{}
    };
  },
  created(){
  },
  mounted(){
  },
  methods: {
    async openCamera(){
      // 检查是否存在设备对象
      const navigator = window.navigator.mediaDevices;
      const devices = await navigator.enumerateDevices();
      console.log('devices',devices);
      if (devices) {
        // 获取媒介流, audio 音频; video 视频;
        this.stream = await navigator.getUserMedia({
          audio: false,
          video: {
            width: 300,
            height: 300,
            facingMode: "user", //前置摄像头
          },
        });
        // 将流推送至video标签
        this.$refs.videoEl.srcObject = this.stream;
        this.$refs.videoEl.play();
      }
    },

    async closeCamera(){
      this.stream.getVideoTracks().forEach(track => {
       track.stop()
      })
      this.$refs.videoEl.srcObject = null;
    }
  },
};